//
//  NearestNeighbor.m
//  Henry
//
//  Created by Ron Olson on 3/12/10.
//  Copyright 2010 Keep Bertha Surfin. All rights reserved.
//

#import "NearestNeighbor.h"


@implementation NearestNeighbor

-(id)initWithWaypoints:(NSArray*)waypointList startingAt:(Waypoint*)firstWaypoint
{
	if (![super init])
		return nil;
	
	waypoints = waypointList;
	originPoint = firstWaypoint;
	
	return self;	
}

-(NSArray*)getSortedWaypoints
{	
	NSMutableArray *originalWaypoints = [[NSMutableArray alloc] initWithArray:waypoints];
	NSMutableArray *sortedWaypoints = [[NSMutableArray alloc] init];
	Waypoint *testWP = originPoint;
	
	while ([originalWaypoints count] > 0) 
	{
		Waypoint *closestWP = [testWP isClosestTo:originalWaypoints];
		[sortedWaypoints addObject:closestWP];
		[originalWaypoints removeObject:closestWP];
		testWP = closestWP;
	}
	
	return sortedWaypoints;
}

@end
